package com.cookor.poi.modules.phy.service;

import com.cookor.poi.common.persistence.DataEntity;
import com.cookor.poi.common.persistence.TreeEntity;
import com.cookor.poi.common.service.TreeService;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.phy.dao.TOfficeDao;
import com.cookor.poi.modules.phy.entity.TDoctor;
import com.cookor.poi.modules.phy.entity.TOffice;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class TOfficeService extends TreeService<TOfficeDao, TOffice> {
  @Autowired
  private TDoctorService tDoctorService;
  
  public TOffice get(String id) {
    return (TOffice)super.get(id);
  }
  
  public List<TOffice> findList(TOffice tOffice) {
    if (StringUtils.isNotBlank(tOffice.getParentIds()))
      tOffice.setParentIds("," + tOffice.getParentIds() + ","); 
    return super.findList( tOffice);
  }
  
  @Transactional(readOnly = false)
  public void save(TOffice tOffice) {
    super.save(tOffice);
  }
  
  @Transactional(readOnly = false)
  public void delete(TOffice tOffice) {
    super.delete( tOffice);
  }
  
  @Transactional(readOnly = false)
  public void myDelete(TOffice tOffice) throws Exception {
    TDoctor tDoctor = new TDoctor();
    tDoctor.setOfficeId(Integer.valueOf(tOffice.getId()));
    List<TDoctor> list = this.tDoctorService.findList(tDoctor);
    if (!list.isEmpty())
      throw new Exception("不能删除，还有医生关联在该诊室下！"); 
    super.delete( tOffice);
  }
}
